/**
 * DemoActivity.java
 * Copyright(C) 2014
 * creator:cuiran 2014-6-11 下午3:34:58
 */
package org.videolan.vlc.gui;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.videolan.libvlc.LibVlcException;
import org.videolan.libvlc.LibVlcUtil;
import org.videolan.vlc.AudioServiceController;
import org.videolan.vlc.Util;
import org.videolan.vlc.gui.audio.AudioPlayer;
import org.videolan.vlc.gui.video.VideoPlayerActivity;
import org.videolan.vlc.widget.SlidingPaneLayout;

import com.actionbarsherlock.app.ActionBar;
import com.slidingmenu.lib.SlidingMenu;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * TODO 
 * @author cuiran
 * @version 1.0.0
 */
public class DemoActivity extends Activity {
	public final static String TAG = "VLC/DemoActivity";
	 protected static final String ACTION_SHOW_PROGRESSBAR = "org.videolan.vlc.gui.ShowProgressBar";
	    protected static final String ACTION_HIDE_PROGRESSBAR = "org.videolan.vlc.gui.HideProgressBar";
	    protected static final String ACTION_SHOW_TEXTINFO = "org.videolan.vlc.gui.ShowTextInfo";
	    public static final String ACTION_SHOW_PLAYER = "org.videolan.vlc.gui.ShowPlayer";

	    private static final String PREF_SHOW_INFO = "show_info";
	    private static final String PREF_FIRST_RUN = "first_run";

	    private static final int ACTIVITY_RESULT_PREFERENCES = 1;
	    private static final int ACTIVITY_SHOW_INFOLAYOUT = 2;

	    private ActionBar mActionBar;
	    private SlidingMenu mMenu;
	    private SidebarAdapter mSidebarAdapter;
	    private AudioPlayer mAudioPlayer;
	    private AudioServiceController mAudioController;
	    private SlidingPaneLayout mSlidingPane;

	    private View mInfoLayout;
	    private ProgressBar mInfoProgress;
	    private TextView mInfoText;
	    private View mAudioPlayerFilling;
	    private String mCurrentFragment;
	    private String mPreviousFragment;
	    private List<String> secondaryFragments = Arrays.asList("albumsSongs", "equalizer",
	                                                            "about", "search", "mediaInfo",
	                                                            "videoGroupList");
	    private HashMap<String, Fragment> mSecondaryFragments = new HashMap<String, Fragment>();

	    private SharedPreferences mSettings;

	    private int mVersionNumber = -1;
	    private boolean mFirstRun = false;
	    private boolean mScanNeeded = true;

	 @Override
	public void onCreate(Bundle savedInstanceState) {
		
		 if (!LibVlcUtil.hasCompatibleCPU(this)) {
	            Log.e(TAG, LibVlcUtil.getErrorMsg());
	            Intent i = new Intent(this, CompatErrorActivity.class);
	            startActivity(i);
	            finish();
	            super.onCreate(savedInstanceState);
	            return;
	        }
		 
		   /* Get the current version from package */
	        PackageInfo pinfo = null;
	        try {
	            pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
	        } catch (NameNotFoundException e) {
	            Log.e(TAG, "package info not found.");
	        }
	        if (pinfo != null)
	            mVersionNumber = pinfo.versionCode;

	        /* Get settings */
	        mSettings = PreferenceManager.getDefaultSharedPreferences(this);

	        /* Check if it's the first run */
	        mFirstRun = mSettings.getInt(PREF_FIRST_RUN, -1) != mVersionNumber;
	        if (mFirstRun) {
	            Editor editor = mSettings.edit();
	            editor.putInt(PREF_FIRST_RUN, mVersionNumber);
	            editor.commit();
	        }

	        try {
	            // Start LibVLC
	            Util.getLibVlcInstance();
	        } catch (LibVlcException e) {
	            e.printStackTrace();
	            Intent i = new Intent(this, CompatErrorActivity.class);
	            i.putExtra("runtimeError", true);
	            i.putExtra("message", "LibVLC failed to initialize (LibVlcException)");
	            startActivity(i);
	            finish();
	            super.onCreate(savedInstanceState);
	            return;
	        }
	        
	        super.onCreate(savedInstanceState);
	        /* Set up the audio player */
	        mAudioPlayer = new AudioPlayer();
	        mAudioController = AudioServiceController.getInstance();
	        mAudioController.addAudioPlayer(mAudioPlayer);
	        AudioServiceController.getInstance().bindAudioService(this);
	        //http://192.168.1.196/video/hao.mpg
	        String filepath="udp://@239.1.1.0:1234";
		 
		   mAudioController.load(filepath, false);
		   mAudioController.play();
		   VideoPlayerActivity.start(this, filepath);
	}
	
}
